package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.StorageLocationType;
import cn.b.sky.mes.mm.model.condition.StorageLocationTypeCondition;
import cn.b.sky.mes.mm.service.StorageLocationTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/storageLocationType/*")
public class StorageLocationTypeController extends SecurityController {
  @Resource
  StorageLocationTypeService storageLocationTypeService;

  @RequestMapping(value = "storageLocationTypeList")
  public String storageLocationTypeList(Model model,
      @ModelAttribute("condition") StorageLocationTypeCondition storageLocationTypeCondition, String navTabId) {
    storageLocationTypeCondition.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(storageLocationTypeCondition.getOrderField())) {
      storageLocationTypeCondition.setOrderField("createDateTime");
    }
    List<StorageLocationType> storageLocationTypes = storageLocationTypeService
        .selectStorageLocationTypeList(storageLocationTypeCondition);
    int count = storageLocationTypeService.selectStorageLocationTypeCount(storageLocationTypeCondition);
    storageLocationTypeCondition.setData(storageLocationTypes);
    storageLocationTypeCondition.setTotalCount(count);
    model.addAttribute("page", storageLocationTypeCondition);
    model.addAttribute("navTabId", navTabId);
    return "mm/storageLocationType/storageLocationType_list";
  }

  @RequestMapping(value = "addStorageLocationType")
  public String addStorageLocationType(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("pattern", "add");
    return "mm/storageLocationType/storageLocationType_add";
  }

  @RequestMapping(value = "modifyStorageLocationType")
  public String modifyStorageLocationType(String navTabId, String id, Model model) {
    StorageLocationType storageLocationType = storageLocationTypeService.selectStorageLocationType(getSiteCode(), id);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("storageLocationType", storageLocationType);
    model.addAttribute("pattern", "modify");
    return "mm/storageLocationType/storageLocationType_add";
  }

  @RequestMapping(value = "viewStorageLocationType")
  public String viewStorageLocationType(String navTabId, String id, Model model) {

    StorageLocationType storageLocationType = storageLocationTypeService.selectStorageLocationType(getSiteCode(), id);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("storageLocationType", storageLocationType);
    model.addAttribute("pattern", "read");
    return "mm/storageLocationType/storageLocationType_add";
  }

  @RequestMapping(value = "validateStorageLocationTypeCode")
  @ResponseBody
  public boolean validateStorageLocationTypeCode(String storageLocationTypeCode, Model model) {
    if (!StringUtils.isBlank(storageLocationTypeCode)) {
      StorageLocationType storageLocationType = storageLocationTypeService.selectStorageLocationType(getSiteCode(),
          storageLocationTypeCode);
      return storageLocationType == null;
    }
    return false;
  }

  @RequestMapping(value = "saveStorageLocationType")
  @ResponseBody
  public DWZResult saveStorageLocationType(Model model, String navTabId, String id,
      StorageLocationType storageLocationType, HttpServletRequest request) {
    if (storageLocationType == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }

    setCreateInfo(storageLocationType);
    storageLocationType.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      storageLocationTypeService.insertStorageLocationType(storageLocationType);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    } else {
      storageLocationTypeService.updateStorageLocationType(storageLocationType);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    }

  }

  @RequestMapping("deleteStorageLocationType")
  @ResponseBody
  public DWZResult deleteStorageLocationType(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = storageLocationTypeService.deleteStorageLocationType(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

}
